#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    // Initial layout,widget setup
    networkClient = new NetworkClient;
    stackedWidget = new QStackedWidget;
    layout = new QVBoxLayout;
    layout->addWidget(stackedWidget);
    setLayout(layout);


    // Default IP/Port
    connect(this, SIGNAL(changeIpPortNumber(const QString &, int)),
            networkClient, SLOT(changeServerAddress(const QString &,int)));
    emit changeIpPortNumber("127.0.0.1", 35382);

    // Launch Log-In Widget
    launchLogInWidget();
}

MainWidget::~MainWidget()
{
    delete networkClient;
    delete stackedWidget;
    delete layout;
    delete ui;
}

void MainWidget::launchLogInWidget()
{
    LogInWidget *logInWidget = new LogInWidget;

    //Widget will be destroyed when closed
    logInWidget->setAttribute(Qt::WA_DeleteOnClose);

    stackedWidget->addWidget(logInWidget);
    stackedWidget->setCurrentWidget(logInWidget);
    this->setBaseSize(350,200);
    qDebug() << "StackedWidget loginWidget: " << stackedWidget->count();

    //connect loginWiget's login signal to networkClient's loginAttempt slot
    connect(logInWidget, SIGNAL(login(UserData *)), networkClient, SLOT(attemptLogIn(UserData *)));
    connect(networkClient, SIGNAL(logInSuccessful(UserData *)), logInWidget, SLOT(handleSuccessfulLogin(UserData *)));
    connect(networkClient, SIGNAL(logInFailed(const QString &)), logInWidget, SLOT(handleLoginError(const QString&)));
    connect(logInWidget, SIGNAL(launchUI(UserData *)), this, SLOT(determineWidget(UserData*)));

    connect(logInWidget, SIGNAL(sendIPPortToMainWidget(QString, int)), networkClient, SLOT(changeServerAddress(QString,int)));

    //ip/prt setting signal
    //connect(this, SIGNAL(changeIpPortNumber(const QString&, int)), networkClient, SLOT(changeServerAddress(const QString&,int)));
}

void MainWidget::determineWidget(UserData *userData)
{
    if(userData->isAdmin())
    {
        qDebug() << "StackedWidget Count2: " << stackedWidget->count();
        managementWidget = new ManagementWidget;
        managementWidget->setOwner(userData->getUserName());
        managementWidget->setAttribute(Qt::WA_DeleteOnClose);
        stackedWidget->addWidget(managementWidget);
        stackedWidget->setCurrentWidget(managementWidget);
        this->setFixedSize(600,450);

        //Load ALLUsers in the system to the left listWidget
        //Singals/slots for loading all users
        connect(managementWidget, SIGNAL(loadUsers(UserData *)), networkClient, SLOT(requestAllUsers(UserData *)));
        connect(networkClient, SIGNAL(userListReady(QStringList*)), managementWidget, SLOT(userList(QStringList*)));
        connect(networkClient, SIGNAL(userListError(const QString &)), managementWidget, SLOT(userListError(const QString&)));
        connect(managementWidget, SIGNAL(disconnectUserListSignal()), this, SLOT(disconnectUserListSignal()));

        //signals/slots for adding/removing users
        connect(managementWidget, SIGNAL(addNewUser(UserData *)), networkClient, SLOT(newUserRequest(UserData *)));
        connect(networkClient, SIGNAL(newUserAdded(const QString &)), managementWidget, SLOT(addNewUserSuccess(const QString &)));
        connect(networkClient, SIGNAL(newUserNotAdded(const QString &)), managementWidget, SLOT(addNewUserFailed(const QString &)));
        connect(managementWidget, SIGNAL(removeUser(UserData *)), networkClient, SLOT(removeUserRequest(UserData *)));
        connect(networkClient, SIGNAL(userRemoved()), managementWidget, SLOT(removeUserSuccess()));
        connect(networkClient, SIGNAL(userRemoveFailed(const QString &)), managementWidget, SLOT(removeUserFailed(const QString &)));

        //signals/slots for adding/removing accessRights
        connect(managementWidget, SIGNAL(addAccessRight(const QString &,const QString &)), networkClient, SLOT(addAccessRequest(const QString &, const QString &)));
        connect(networkClient, SIGNAL(accessAddSuccess()), managementWidget, SLOT(addAccessRightSuccess()));
        connect(networkClient, SIGNAL(accessAddFailed(const QString &)), managementWidget, SLOT(addAccessRightFailed(const QString &)));
        connect(managementWidget, SIGNAL(removeAccessRight(const QString &,const QString &)), networkClient, SLOT(removeAccessRequest(const QString &, const QString &)));
        connect(networkClient, SIGNAL(accessRemoveSuccess()), managementWidget, SLOT(removeAccessRightSuccess()));
        connect(networkClient, SIGNAL(accessRemoveFailed(const QString &)), managementWidget, SLOT(removeAccessRightFailed(const QString &)));

        qDebug() << "Calling requestAllUsers()*******************************";
        networkClient->requestAllUsers(userData);


        this->setWindowTitle("Currently Logged-In as a manager: " + userData->getUserName());

        //Signal/Slot
        //Create ManagementWidget object..
        //stackedWidget->setCurrentWidget();
        //Delete LoginWidget in stackedwidget
        //widget->setAttribute(Qt::WA_DeleteOnClose);
    }
    else
    {
        qDebug() << "StackedWidget Count3: " << stackedWidget->count();

        // Summary of this section
        //Create ClientWidget object..
        //stackedWidget->setCurrentWidget();
        //connect signals and slots..
        //Delete LoginWidget in stackedwidget
        //widget->setAttribute(Qt::WA_DeleteOnClose);

        clientWidget = new ClientWidget;
        clientWidget->setOwner(userData->getUserName());
        clientWidget->setAttribute(Qt::WA_DeleteOnClose);
        //stackedWidget->removeWidget();
        stackedWidget->addWidget(clientWidget);
        stackedWidget->setCurrentWidget(clientWidget);
        this->setFixedSize(500,600);
        qDebug() << "StackedWidget ClientWidget: " << stackedWidget->count();

        //Signal/Slot for retrieving user's/other's log
        connect(clientWidget, SIGNAL(loadLogs(QStringList *)), networkClient, SLOT(requestLogs(QStringList *)));
        //connect(this, SIGNAL(loadLogs(QString username)), networkClient, SLOT(requestLogs(QString username)));
        connect(networkClient, SIGNAL(logReadyToRead(LogEntry *)), clientWidget, SLOT(readLog(LogEntry *)));
        connect(networkClient, SIGNAL(logReadError(const QString&)), clientWidget, SLOT(readLogError(const QString&)));

        //Signal/slot for Adding New Entry
        connect(clientWidget, SIGNAL(newEntry(LogEntry*)), networkClient, SLOT(sendNewLog(LogEntry*)));
        connect(networkClient, SIGNAL(newLogEntrySuccessful(const QString &)),
                clientWidget, SLOT(addNewEntrySuccessful(const QString &)));
        connect(networkClient, SIGNAL(newLogEntryError(const QString&)), clientWidget, SLOT(addNewEntryError(const QString&)));

        connect(clientWidget, SIGNAL(loadUsers(UserData *)), networkClient, SLOT(requestAllUsers(UserData *)));
        connect(networkClient, SIGNAL(userListReady(QStringList*)), clientWidget, SLOT(userList(QStringList*)));
        connect(networkClient, SIGNAL(userListError(const QString &)), clientWidget, SLOT(userListError(const QString &)));

        qDebug() << "loadLogList called";
        clientWidget->loadLogList();
        clientWidget->loadUsersBox(userData);
        this->setWindowTitle("Currently Logged-In as " + userData->getUserName());
    }
}

void MainWidget::disconnectUserListSignal()
{
    qDebug() << "disconnectUserListSignal and connect to accessListsignal";
    disconnect(networkClient, SIGNAL(userListReady(QStringList*)), managementWidget, SLOT(userList(QStringList*)));
    disconnect(networkClient, SIGNAL(userListError(QString)), managementWidget, SLOT(userListError(QString)));
    connect(managementWidget, SIGNAL(loadAccessList(UserData*)), networkClient, SLOT(requestAllUsers(UserData*)));
    connect(networkClient, SIGNAL(userListReady(QStringList*)), managementWidget, SLOT(accessList(QStringList*)));
    connect(networkClient, SIGNAL(userListError(QString)), managementWidget, SLOT(accessListError(QString)));
}
